package com.marvels.financial.loan.controller;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.loan.dto.PaymentFlowConditionDTO;
import com.marvels.financial.loan.dto.PaymentFlowDetailDTO;
import com.marvels.financial.loan.dto.TrialCostConditionDTO;
import com.marvels.financial.loan.service.BalanceAccountApi;


/**
 * 流水对账
 * @author tangpw
 *
 */
@RestController
@RequestMapping("financial/loan/balanceAccount/service")
public class BalanceAccountController {
	
	@Autowired
	private BalanceAccountApi balanceAccountApi;
	
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	
	
	/**
	 * list的筛选条件
	 * @param listQueryConditionDto
	 * @return
	 */
	@RequestMapping("listScreenCondition")
	@ResponseBody
	public MarvelsResultDTO listScreenCondition() {
		try {
			Map<String, Object> map = new HashMap<>();
			map = balanceAccountApi.listScreenCondition();
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					map);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>清单查询list的筛选FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>清单查询list的筛选条件:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}

	}
	
	@RequestMapping("/listQuery")
	@ResponseBody
	public MarvelsResultDTO listQuery(@RequestBody PaymentFlowConditionDTO conditionDTO){
		try {
			Map<String, Object> data = balanceAccountApi.listQuery(conditionDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), data);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询提前结清申请数据FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询提前结清申请数据异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询提前结清申请数据异常", null);
		}
	}
	
	
	/**
	 * 下载excel
	 * @param conditionDTO
	 * @param request
	 * @param response
	 */
	@RequestMapping("/downLoad")
	public void downLoad(PaymentFlowConditionDTO conditionDTO,HttpServletRequest request,HttpServletResponse response){
		OutputStream outs = null;
		try {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition",
					"attachment; filename=" + new String("还款流水详情.xls".getBytes("UTF-8"), "ISO8859-1"));
			HSSFWorkbook hssFWorkbook = balanceAccountApi.downLoad(conditionDTO);
			outs = response.getOutputStream();// 获取文件输出IO流
			hssFWorkbook.write(outs);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>还款流水下载excel失败:", e);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>还款流水下载excel异常", e);
		}finally {
			try {
				outs.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * 征审费用收取流水详情
	 * @param conditionDTO
	 * @return
	 */
	@RequestMapping("/trialCostList")
	@ResponseBody
	public MarvelsResultDTO trialCostList(@RequestBody TrialCostConditionDTO conditionDTO){
		try {
			return balanceAccountApi.trialCostList(conditionDTO);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>征审费用收取流水详情查询数据FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>征审费用收取流水详情查询异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "征审费用收取流水详情查询异常", null);
		}
	}
	
	
	
	
	/**
	 * 征审费用收取流水下载excel
	 * @param conditionDTO
	 * @return
	 */
	@RequestMapping("/downLoadTrialCost")
	@ResponseBody
	public void trialCostDownLoad(TrialCostConditionDTO conditionDTO,HttpServletRequest request,HttpServletResponse response){
		OutputStream outs = null;
		try {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition",
					"attachment; filename=" + new String("信贷审批费用明细.xls".getBytes("UTF-8"), "ISO8859-1"));
			HSSFWorkbook hssFWorkbook = balanceAccountApi.trialCostDownLoad(conditionDTO);
			outs = response.getOutputStream();// 获取文件输出IO流
			hssFWorkbook.write(outs);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>征审费用收取流水下载excel失败:", e);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>征审费用收取流水下载excel异常", e);
		}finally{
			try {
				outs.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * 征审费用汇总详情下载
	 * @param conditionDTO
	 * @return
	 */
	@RequestMapping("/downLoadTrialCostSummary")
	@ResponseBody
	public void trialCostSummaryDownLoad(TrialCostConditionDTO conditionDTO,HttpServletRequest request,HttpServletResponse response){
		OutputStream outs = null;
		try {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition",
					"attachment; filename=" + new String("征审费收入汇总.xls".getBytes("UTF-8"), "ISO8859-1"));
			HSSFWorkbook hssFWorkbook = balanceAccountApi.trialCostCollectDownLoad(conditionDTO);
			outs = response.getOutputStream();// 获取文件输出IO流
			hssFWorkbook.write(outs);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>征审费用汇总详情下载excel失败:", e);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>征审费用汇总详情下载excel异常", e);
		}finally{
			try {
				outs.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
